package com.njtoyo.taxi.admin.service.data.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.library.common.util.ChartUtil;
import com.njtoyo.taxi.admin.library.common.util.CopyUtil;
import com.njtoyo.taxi.admin.logic.allowance.OrderLogic;
import com.njtoyo.taxi.admin.mapper.master.mapper.ScanRideOrderMapper;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.cache.CacheAllowanceCustomerProportionMapper;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.cache.CacheAllowanceDriverMapper;
import com.njtoyo.taxi.admin.rest.presenter.common.ChartCyclesTimeListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.OverviewAllowanceChartPresenter;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.AllowanceStatsPresenter;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.CustomerProportionPresenter;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.OverviewRecommendChartPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceChartWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceWrapper;
import com.njtoyo.taxi.admin.service.data.AllowanceService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
@DS(Const.SECONDARY_DS)
public class AllowanceServiceCacheImpl implements AllowanceService {

    @Autowired
    private CacheAllowanceDriverMapper cacheAllowanceDriverMapper;

    @Autowired
    private OrderLogic orderLogic;

    @Autowired
    private CacheAllowanceCustomerProportionMapper cacheAllowanceCustomerProportionMapper;

    @Autowired
    private ScanRideOrderMapper scanRideOrderMapper;

    @Override
    public RestResult<AllowanceStatsPresenter> main(AdminUser adminUser, AllowanceWrapper wrapper) {
        String start = DateUtil.format(wrapper.getStartTime(), DatePattern.NORM_DATE_PATTERN);
        String end = DateUtil.format(wrapper.getEndTime(), DatePattern.NORM_DATE_PATTERN);

        // 司机补贴数据
        AllowanceStatsPresenter res = cacheAllowanceDriverMapper.getDriverStats(start, end);

        // 乘客补贴数据

        // 扫码订单数量
        Integer scanRideOrderCount = orderLogic.getScanRideOrderCount(wrapper.getStartTime(), wrapper.getEndTime());

        // 乘客数据
        CustomerProportionPresenter customerProportion = cacheAllowanceCustomerProportionMapper.selectCache(start, end);

        // 乘客补贴总额（4个相加）
        BigDecimal customerAllowance = NumberUtil.add(customerProportion.getCouponAmount(), customerProportion.getAllowanceAmount(), customerProportion.getBalanceAmount(), customerProportion.getWechatAmount());

        // 乘客推广总人数
        Integer customerRecommendCount = customerProportion.getRecommendationNum();

        res.setCustomerAllowance(customerAllowance);

        // 加上乘客推广人数
        res.setRecommendCount(res.getRecommendCount() + customerRecommendCount.longValue());
        // 乘客推广总金额
        res.setCustomerRecommend(customerProportion.getRecommendationAmount());
        res.setScanCount(scanRideOrderCount.longValue());
        return RestResult.success(res);
    }

    @Override
    public RestResult<List<OverviewAllowanceChartPresenter>> totalChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        List<OverviewAllowanceChartPresenter> customerList = cacheAllowanceCustomerProportionMapper.selectAllowanceChart(wrapper);
        List<OverviewAllowanceChartPresenter> driverList = cacheAllowanceDriverMapper.selectAllowanceChart(wrapper);

        Long startTimeStamp = DateUtil.beginOfDay(wrapper.getStartDate()).getTime() / 1000;
        Long endTimeStamp = DateUtil.endOfDay(wrapper.getEndDate()).getTime() / 1000;
        List<OverviewAllowanceChartPresenter> scanRideOrderList = scanRideOrderMapper.selectScanRideOrderChart(startTimeStamp, endTimeStamp);

        List<ChartCyclesTimeListPresenter> timeList = ChartUtil.getCyclesByCustomizeDaily(wrapper.getStartDate(), wrapper.getEndDate());
        List<OverviewAllowanceChartPresenter> timeListConv = CopyUtil.copyList(timeList, OverviewAllowanceChartPresenter.class);

        for (OverviewAllowanceChartPresenter timeItem : timeListConv) {
            for (OverviewAllowanceChartPresenter customer : customerList) {
                if (timeItem.getStartTime().equals(customer.getStartTime())) {
                    timeItem.setCustomerAllowanceAmount(customer.getCustomerAllowanceAmount());
                }
            }

            for (OverviewAllowanceChartPresenter driver : driverList) {
                if (timeItem.getStartTime().equals(driver.getStartTime())) {
                    timeItem.setDriverAllowanceAmount(driver.getDriverAllowanceAmount());
                    timeItem.setOrderCount(driver.getOrderCount());
                }
            }

            for (OverviewAllowanceChartPresenter scanRideOrder : scanRideOrderList) {
                if (timeItem.getStartTime().equals(scanRideOrder.getStartTime())) {
                    timeItem.setScanCount(scanRideOrder.getScanCount());
                }
            }
        }

        return RestResult.success(timeListConv);
    }

    @Override
    public RestResult<List<OverviewRecommendChartPresenter>> recommendChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        List<OverviewRecommendChartPresenter> customerList = cacheAllowanceCustomerProportionMapper.selectRecommendChart(wrapper);
        List<OverviewRecommendChartPresenter> driverList = cacheAllowanceDriverMapper.selectRecommendChart(wrapper);

        List<OverviewRecommendChartPresenter> list = new ArrayList<>(customerList);
        list.addAll(driverList);

        List<ChartCyclesTimeListPresenter> timeList = ChartUtil.getCyclesByCustomizeDaily(wrapper.getStartDate(), wrapper.getEndDate());
        List<OverviewRecommendChartPresenter> timeListConv = CopyUtil.copyList(timeList, OverviewRecommendChartPresenter.class);

        for (OverviewRecommendChartPresenter timeItem : timeListConv) {
            for (OverviewRecommendChartPresenter resItem : list) {
                if (timeItem.getStartTime().equals(resItem.getStartTime())) {
                    timeItem.setCustomerRecommendCount(resItem.getCustomerRecommendCount());
                    timeItem.setCustomerRecommendAmount(resItem.getCustomerRecommendAmount());
                    timeItem.setDriverRecommendCount(resItem.getDriverRecommendCount());
                    timeItem.setDriverRecommendAmount(resItem.getDriverRecommendAmount());
                }
            }
        }

        return RestResult.success(timeListConv);
    }
}
